// pages/componentSm/login/login.js
import WxValidate from '../../../utils/WxValidate.js';

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    loginChange:{
      type:Boolean,
      value:true
    }
  },
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的初始数据
   */
  data: {
    form: {
      logName: "",  //用户名
      logPwd: "",     //密码
      logPhone: "",   //手机号
      vfCode: "",  //验证码 
    }
  },
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
      this.initValidate1()//账号密码验证规则函数
      this.initValidate2()//手机短信验证规则函数
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //报错
    showModal(error) {
      wx.showModal({
        content: error.msg,
        showCancel: false,
      })
    },
    //账号密码登录验证函数
    initValidate1() {
      const rules = {
        logName: {
          required: true,
          minlength:2
        },
        logPwd: {
          required: true,
          minlength: 6
        }
      }
      const message = {
        logName: {
          required: '请填写用户名',
          minlength: '请填写正确的用户名'
        },
        logPwd: {
          required: '请填写密码',
          minlength: '请填写正确的密码'
        }
      }
      this.WxValidate1 = new WxValidate(rules, message);
    },
    //调用验证函数
    formSubmit1: function (e) {
      const params = e.detail.value

      //校验表单
      if (!this.WxValidate1.checkForm(params)) {
        const error = this.WxValidate1.errorList[0]
        this.showModal(error)
        return false
      }
      this.showModal({
        msg: '提交成功'
      })
    },

    //手机短信登录验证
    //验证函数
    initValidate2() {
      const rules = {
        logPhone: {
          required: true,
          tel: true
        },
        vfCode: {
          required: true,
          digits: true,
          minlength: 6
        }
      }
      const message = {
        logPhone: {
          required: '请填写手机号',
          tel: '请填写正确的手机号码'
        },
        vfCode: {
          required: '请填写验证码',
          digits: '验证码只能为数字',
          minlength: '请填写正确的验证码'
        }
      }
      this.WxValidate2 = new WxValidate(rules, message);
    },
    //调用验证函数
    formSubmit2: function (e) {
      const params = e.detail.value

      //校验表单
      if (!this.WxValidate2.checkForm(params)) {
        const error = this.WxValidate2.errorList[0]
        this.showModal(error)
        return false
      }
      this.showModal({
        msg: '提交成功'
      })
    },
  }
})
